package me.sdevil507.org.po;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import me.sdevil507.platform.po.PlatDeptPo;
import me.sdevil507.platform.po.PlatResourcePo;
import me.sdevil507.supports.jpa.BaseJpaSnowflakeIdAndAuditEntity;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * 机构[OrgPo]
 *
 * @author sdevil507
 * created on 2020-07-13 13:40:20
 */
@Entity
@Table(name = "biz_org")
@Data
@EqualsAndHashCode(callSuper = true, exclude = {"orgUserRel", "roles", "resources", "depts", "wordCloud", "presetMgr"})
@ToString(exclude = {"orgUserRel", "roles", "depts", "wordCloud", "presetMgr"})
public class OrgPo extends BaseJpaSnowflakeIdAndAuditEntity {

    /**
     * 中文名称
     */
    @Column
    private String nameZh;

    /**
     * 中文简称
     */
    @Column
    private String shortNameZh;

    /**
     * 英文名称
     */
    @Column
    private String nameEn;

    /**
     * 英文简称
     */
    @Column
    private String shortNameEn;

    /**
     * 简介
     */
    @Column
    private String intro;

    /**
     * 登录页logo
     */
    @Column
    private String logo;
    /**
     * 主页homeLogo
     */
    @Column
    private String homeLogo;
    /**
     * slogan
     */
    @Column
    private String slogan;
    /**
     * 登录页背景图
     */
    @Column
    private String bgkImgUrl;

    /**
     * 智库地址
     */
    @Column
    private String address;

    /**
     * 创立时间
     */
    @Column
    private Date establishDate;

    /**
     * 逻辑删除标记
     */
    @Column
    private Boolean deleted;

    /**
     * 是否启用
     */
    @Column
    private boolean enable;

    /**
     * 主管单位
     */
    @Column
    private String directorInstitution;

    /**
     * 直属单位
     */
    @Column
    private String underInstitution;

    /**
     * 是否审批通过
     */
    @Column
    private boolean approve;

    /**
     * 省id
     */
    @Column
    private Integer provinceId;

    /**
     * 市id
     */
    @Column
    private Integer cityId;

    /**
     * 区id
     */
    @Column
    private Integer districtId;

    /**
     * 是否具有法人资格
     */
    @Column
    private String legalPersonality;

    /**
     * 法人类型
     */
    @Column
    private String legalPersonType;

    /**
     * 参加的智库联盟
     */
    @Column(length = 1000)
    private String league;

    /**
     * 智库类型
     */
    @Column(length = 1000)
    private String type;
    @Formula("(select t.d_name from biz_sysdict as t  where t.d_value=type and t.type_key='ThinkTankType' and t.is_del = 0)")
    private String typeName;

    /**
     * 智库类别
     */
    @Column(length = 1000)
    private String classify;
//    @Formula("(select GROUP_CONCAT(t.d_name) from biz_sysdict as t  where t.d_value in (classify) and t.type_key='ThinkTankClassify' and t.is_del = 0 group by t.type_key)")
//    private String classifyName;

    /**
     * 研究领域
     */
    @Column(length = 1000)
    private String domain;
//    @Formula("(select t.d_name from biz_sysdict as t  where t.d_value=domain and t.type_key='ResearchDomain' and t.is_del = 0)")
//    private String domainName;

    /**
     * 涉及学科
     */
    @Column(length = 1000)
    private String subject;

    /**
     * 域名前缀
     */
    @Column
    private String preDomainName;

    /**
     * 挂靠单位
     */
    @Column
    private String attachedUnits;

    /**
     * 首席专家
     */
    @Column
    private String chiefExpert;

    /**
     * 官网地址
     */
    @Column
    private String websiteAddress;

    /**
     * 官网名称
     */
    @Column
    private String websiteAddressName;

    /**
     * 公众号
     */
    @Column
    private String officialAccounts;

    /**
     * 公众号名称
     */
    @Column
    private String officialAccountsName;

    /**
     * CTTI-key
     */
    @Column
    private String cttiApiKey;

    /**
     * 包含关键词
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "org")
    @org.hibernate.annotations.ForeignKey(name = "none")
    @OrderBy("createdDate ASC")
    private Set<OrgKeywordPo> keywords;

    /**
     * 机构用户关联关系
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "org", cascade= CascadeType.ALL)
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Set<OrgUserRelPo> orgUserRel;

    /**
     * 包含角色
     */
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "orgs")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Set<OrgRolePo> roles;

    /**
     * 包含资源
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "biz_org_resource_rel",
            joinColumns = {
                    @JoinColumn(name = "org_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))},
            inverseJoinColumns = {
                    @JoinColumn(name = "res_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))})
    private Set<PlatResourcePo> resources;

    /**
     * 包含部门
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "org")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Set<OrgDeptPo> depts;

    /**
     * 预置管理员
     */
    @OneToOne
    @JoinColumn(name = "mgr_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    private OrgUserPo presetMgr;

}